package service

import (
	"context"
	"fmt"
	"micro-grpc-server/library/image2"
	"micro-grpc-server/services/user/internal/models"
	"micro-grpc-server/services/user/internal/resource"
	pb "micro-grpc-server/services/user/proto"
	wechat "micro-grpc-server/services/wechat/proto"
)

// 用户列表
func (s *UserService) UserList(ctx context.Context, rq *pb.ListUserRequest, resp *pb.ListUserResponse) (err error) {
	var list []*models.Identity
	list, resp.Total, err = s.dao.UserList(rq)
	if err != nil {
		return err
	}

	var r = &resource.UserResource{}
	for _, user := range list {
		resp.List = append(resp.List, r.UserToProto(user))
	}

	return nil
}

// 用户信息
func (s *UserService) UserProfile(ctx context.Context, rq *pb.UserIdRequest, resp *pb.User) (err error) {
	var identity *models.Identity
	identity, err = s.dao.FindUserByUserId(rq.UserId)
	if err != nil {
		return err
	}
	var r = &resource.UserResource{}
	resp = r.UserToProto(identity)

	return nil
}

// 用户分享图
func (s *UserService) UserShare(ctx context.Context, rq *pb.UserIdRequest, resp *pb.UserShareResponse) (err error) {
	var img = image2.NewCanvasImage(&image2.Canvas{
		Path:   "assets/images/activity_share_front.png",
		Width:  630,
		Height: 954,
		//Color:    "#FF0000",
		FontFile: "assets/fonts/PingFang-SC-Regular.ttf",
	})
	img.WriteText("静待", 94, 32, 5, 20, 28, "#323232", 374, 1, nil)
	raw, _ := img.WriteText("【月卡版】OLBOX-3 PR", 20, 726, 0, 34, 48, "#000000", 374, 2, nil)
	img.WriteText("【月卡版】OLBOX-3 PRO ", 20, 726+48*raw, 0, 26, 37, "#A6A6A6", 374, 1, nil)
	img.WriteText("免费试用", 20, 726+48*raw+57, 8, 22, 30, "#FFFFFF", 104, 1, &image2.StringArgs{
		TextBackground: &image2.TextBackground{Color: "#0101FD"},
	})
	img.WriteText("市场价: 199元", 134, 726+48*raw+53, 0, 24, 34, "A6A6A6", 160, 1, &image2.StringArgs{
		LineThrough: &image2.LineThrough{Color: "#A6A6A6", Height: 1, OffsetX: 12*7},
	})
	img.JoinHandlers(&image2.Picture{
		Path: "https://t.g.mg-cdn.com/avatar/14/up_5e9e597f368808.72933813", X: 20, Y: 30, Width: 66, Height: 66, Circle: true,
	})
	img.JoinHandlers(&image2.Picture{
		Path: "https://t.g.mg-cdn.com/uploads/static/4/202011/up_5fa24a42d53c81.23902341.png", X: 20, Y: 116, Width: 590, Height: 590,
	})
	wechatCodeReq := &wechat.WechatQrcodeRequest{
		Scenario: "testing_wxapp",
		Page:     "pages/activity/detail",
		Width:    200,
		Scene:    fmt.Sprintf("id=%d&type=activity", 5),
	}
	code, err := s.wechatCli.WechatCode(ctx, wechatCodeReq)
	if err != nil {
		return err
	}
	img.JoinHandlers(&image2.Buffer{
		Buf: code.Image, X: 459, Y: 738, Width: 140, Height: 140,
	})
	resp.Image, err = img.WriteToBytes()
	return nil
}
